package com.wf.ew.orderB.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.orderB.biz.CarBiz;
import com.wf.ew.orderB.model.Car;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-04-13 13:52:27
 * @version ：1.0
 */
@Repository
public class CarBizImpl extends BaseDao implements CarBiz {

	@Override
	public String list(PageResult<T> page, Car car, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Car> rm = new BeanPropertyRowMapper<Car>(Car.class);
		StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.carNo");
        sql.append(",c.fleet,c.driver,c.driverPhone,c.qrcode,c.orderNum");
		sql.append(",FORMAT((select avg(s.score) from sszg_score s where s.carNo=c.carNo and s.del=0),0) score");
		sql.append(",c.relUserId,c.createTime,c.createUser,c.updateTime");
		sql.append(",c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_car c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
//			sql.append(" and c.createUser=:createUser ");
//			map.put("createUser", loginUser.getId());
        }
    	if(Utils.isNotEmpty(car)){
			if(Utils.isNotEmpty(car.getCarNo())){
				sql.append(" and instr(c.carNo,:carNo)>0 ");
				map.put("carNo", car.getCarNo());
			}
			if(Utils.isNotEmpty(car.getSort())&&Utils.isNotEmpty(car.getOrder())){
				sql.append(" order by "+car.getSort()+" "+car.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Car> list(Car car, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Car> rm = new BeanPropertyRowMapper<Car>(Car.class);
        StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.carNo");
        sql.append(",c.fleet,c.driver, c.driverPhone,c.qrcode,c.orderNum");
		sql.append(",FORMAT((select avg(s.score) from sszg_score s where s.carNo=c.carNo and s.del=0),0) score");
		sql.append(",c.relUserId,c.createTime,c.createUser,c.updateTime");
		sql.append(",c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_car c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and c.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
//			sql.append(" and c.createUser=:createUser ");
//			map.put("createUser", loginUser.getId());
        }
    	if(Utils.isNotEmpty(car)){
			if(Utils.isNotEmpty(car.getCarNo())){
				sql.append(" and instr(c.carNo,:carNo)>0 ");
				map.put("carNo", car.getCarNo());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Car getById(Car car) {
		RowMapper<Car> rm = new BeanPropertyRowMapper<Car>(Car.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.companyId,c.carNo");
        sql.append(",c.fleet,c.driver, c.driverPhone,c.qrcode,c.orderNum");
		sql.append(",FORMAT((select avg(s.score) from sszg_score s where s.carNo=c.carNo and s.del=0),0) score");
		sql.append(",c.relUserId,c.createTime,c.createUser,c.updateTime");
		sql.append(",c.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from sszg_car c ");
        sql.append(" inner join sys_user suser on suser.id=c.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=c.companyId ");
        sql.append(" where c.del=0 and cmy.del=0 and c.id=:id ");
		map.put("id", car.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Car car, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into sszg_car (");
        sql.append("companyId,carNo,fleet,driver,driverPhone");
		sql.append(",qrcode,orderNum,relUserId,createUser) values ( ");
        sql.append(":companyId,:carNo,:fleet,:driver,:driverPhone");
		sql.append(",:qrcode,:orderNum,:relUserId,:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("carNo", car.getCarNo());
        map.put("fleet", car.getFleet());
        map.put("driver", car.getDriver());
        map.put("driverPhone", car.getDriverPhone());
        map.put("qrcode", car.getQrcode());
        map.put("orderNum", car.getOrderNum()==null?null:car.getOrderNum()+"");
        map.put("relUserId", car.getRelUserId());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Car car, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update sszg_car set ");
        sql.append("carNo=:carNo,fleet=:fleet,driver=:driver, driverPhone=:driverPhone ");
		sql.append(",qrcode=:qrcode,orderNum=:orderNum,relUserId=:relUserId ");
		sql.append(",updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("carNo", car.getCarNo());
        map.put("fleet", car.getFleet());
        map.put("driver", car.getDriver());
        map.put("driverPhone", car.getDriverPhone());
        map.put("qrcode", car.getQrcode());
        map.put("orderNum", car.getOrderNum()==null?null:car.getOrderNum()+"");
        map.put("relUserId", car.getRelUserId());
		map.put("updateUser", loginUser.getId());
		map.put("id", car.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Car car, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(car.getIds().length()>0){
			for(String ID : car.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update sszg_car set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
